/**
 * @file ui_img_btn.c
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2023-06-13
 * 
 * @copyright Copyright (c) 2023
 * 
 */

#include "ui_img_btn.h"


LV_FONT_DECLAER(lv_font_montserrat_20);


lv_obj_t *ui_img_btn_create(lv_obj_t *parent, void *img, char *title, ui_img_btn_cb cb)
{

    lv_obj_t *img_btn_obj = lv_img_create(parent);
    if(img_btn_obj == NULL)
    {
        COMLOG_ERROR_LOG("create lvgl obj fail\n");
        return NULL;
    }

    lv_disp_t *d_disp = lv_disp_get_default();  
    lv_obj_align(img_btn_obj, LV_ALIGN_CENTER, 0, -d_disp->driver->ver_res * 0.25);
    lv_img_set_src(img_btn_obj, img);
    lv_obj_add_flag(img_btn_obj,LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_event_cb(img_btn_obj, cb, LV_EVENT_CLICKED, NULL);


    lv_anim_t img_btn_a;
    lv_anim_init(&img_btn_a);
    lv_anim_set_exec_cb(&img_btn_a, (lv_anim_exec_xcb_t) lv_obj_set_y); 
    lv_anim_set_var(&img_btn_a, img_btn_obj); 
    lv_anim_set_time(&img_btn_a, 800);
    lv_anim_set_values(&img_btn_a, -d_disp->driver->ver_res * 0.25, -d_disp->driver->ver_res * 0.1);
    lv_anim_set_path_cb(&img_btn_a, lv_anim_path_ease_out);
    lv_anim_start(&img_btn_a); 

    lv_obj_t* label_title = lv_label_create(parent);
    if(label_title == NULL)
    if(img_btn_obj == NULL)
    {
        COMLOG_ERROR_LOG("create lvgl obj fail\n");
        lv_obj_del(img_btn_obj);
        return NULL;
    }
    lv_obj_align(label_title, LV_ALIGN_CENTER, -d_disp->driver->hor_res*0.25,  40);
    lv_label_set_text(label_title, title);
    lv_obj_set_style_text_color(label_title, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(label_title, &lv_font_montserrat_20, 0);

    lv_anim_t label_a;
    lv_anim_init(&label_a);
    lv_anim_set_exec_cb(&label_a, (lv_anim_exec_xcb_t) lv_obj_set_x); 
    lv_anim_set_var(&label_a, label_title); 
    lv_anim_set_time(&label_a, 800);
    lv_anim_set_values(&label_a, -d_disp->driver->hor_res*0.25, 0);
    lv_anim_set_path_cb(&label_a, lv_anim_path_ease_out);
    lv_anim_start(&label_a); 
  
    //paint line 
    //(x,y) (x+img_width, y) (x, y+img_height) (x+img_width, y+img_height) 
    //(x-5, y) (x-5,y-5) (x, y-5)
    int img_height = lv_obj_get_height(img_btn_obj);
    int  img_width = lv_obj_get_width(img_btn_obj);
    int x = lv_obj_get_x(img_btn_obj);  
    int y = lv_obj_get_y(img_btn_obj);  
   
    return img_btn_obj;
}